package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryMemberVirtualImageReq;
import com.sg.dto.base.component.req.QueryMemberVirtualImageDetailReq;
import com.sg.dto.base.component.req.QueryMemberVirtualImageListReq;
import com.sg.entity.OmsMessageMemberVirtualImage;
import com.sg.service.base.component.MOmsMessageMemberVirtualImageService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageMemberVirtualImageMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageMemberVirtualImageServiceImpl
        extends ServiceImpl<OmsMessageMemberVirtualImageMapper, OmsMessageMemberVirtualImage>
        implements MOmsMessageMemberVirtualImageService {

    @Autowired
    OmsMessageMemberVirtualImageMapper omsMessageMemberVirtualImageMapper;

    /**
     * 2-5-07新增成员虚拟形象[6735]
     * gen by moon at 5/17/2023, 3:40:54 AM
     */
    @Trace(operationName = "2-5-07新增成员虚拟形象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addMemberVirtualImage(OmsMessageMemberVirtualImage omsMessageMemberVirtualImage) {
        omsMessageMemberVirtualImage.setMemberVirtualImageId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessageMemberVirtualImage.setIsArchive("FALSE");
        omsMessageMemberVirtualImage.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsMessageMemberVirtualImage.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsMessageMemberVirtualImage != null && omsMessageMemberVirtualImage.getSubjectLifeCycle() != null && omsMessageMemberVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageMemberVirtualImage.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsMessageMemberVirtualImage.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsMessageMemberVirtualImage.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsMessageMemberVirtualImage != null && omsMessageMemberVirtualImage.getSubjectLifeCycle() != null && omsMessageMemberVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageMemberVirtualImage.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsMessageMemberVirtualImage.setIsValid("FALSE");
        omsMessageMemberVirtualImage.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsMessageMemberVirtualImage)) {
            // TODO: 2023/5/17 不知道为什么生成错误
            return omsMessageMemberVirtualImage.getMemberVirtualImageId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-07修改成员虚拟形象[6736]
     * gen by moon at 5/16/2023, 6:37:04 PM
     */
    @Trace(operationName = "2-5-07修改成员虚拟形象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMemberVirtualImage(OmsMessageMemberVirtualImage omsMessageMemberVirtualImage) {
        omsMessageMemberVirtualImage.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsMessageMemberVirtualImage != null && omsMessageMemberVirtualImage.getSubjectLifeCycle() != null && omsMessageMemberVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageMemberVirtualImage.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsMessageMemberVirtualImage != null && omsMessageMemberVirtualImage.getSubjectLifeCycle() != null && omsMessageMemberVirtualImage.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsMessageMemberVirtualImage.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsMessageMemberVirtualImage.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsMessageMemberVirtualImage != null && omsMessageMemberVirtualImage.getSubjectLifeCycle() != null && omsMessageMemberVirtualImage.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsMessageMemberVirtualImage.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsMessageMemberVirtualImage != null && omsMessageMemberVirtualImage.getSubjectLifeCycle() != null && omsMessageMemberVirtualImage.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsMessageMemberVirtualImage.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsMessageMemberVirtualImage);
    }

    /**
     * 2-5-07删除成员虚拟形象[6737]
     * gen by moon at 5/21/2023, 6:24:46 PM
     */
    @Trace(operationName = "2-5-07删除成员虚拟形象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMemberVirtualImage(String memberVirtualImageId) {

        return super.removeById(memberVirtualImageId);
    }

    /**
     * 2-5-07查询成员虚拟形象详情[6738]
     * gen by moon at 5/16/2023, 6:37:08 PM
     */
    @Trace(operationName = "2-5-07查询成员虚拟形象详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessageMemberVirtualImage queryMemberVirtualImageDetail(QueryMemberVirtualImageDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsMessageMemberVirtualImage> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageMemberVirtualImage> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessageMemberVirtualImage retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-07查询成员虚拟形象列表[6739]
     * gen by moon at 6/20/2023, 6:09:07 PM
     */
    @Trace(operationName = "2-5-07查询成员虚拟形象列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageMemberVirtualImage> queryMemberVirtualImageList(QueryMemberVirtualImageListReq reqDto) {

        try {
            QueryWrapper<OmsMessageMemberVirtualImage> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMemberVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMemberVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMemberVirtualImage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsMessageMemberVirtualImage>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-07批量查询成员虚拟形象ByCodes[7300]
     * gen by moon at 7/19/2023, 12:53:38 AM
     */
    @Trace(operationName = "2-5-07批量查询成员虚拟形象ByCodes")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageMemberVirtualImage> batchQueryMemberVirtualImage(BatchQueryMemberVirtualImageReq reqDto) {

        try {
            QueryWrapper<OmsMessageMemberVirtualImage> queryWrapper = new QueryWrapper<>();

            if (reqDto.getMemberVirtualImageList() == null || reqDto.getMemberVirtualImageList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("virtual_image_code", reqDto.getMemberVirtualImageList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMemberVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMemberVirtualImage.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMemberVirtualImage.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageMemberVirtualImage>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
